package com.kgc.warehouse.bussness.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.warehouse.bussness.entity.Customer;
import com.kgc.warehouse.bussness.entity.Goods;
import com.kgc.warehouse.bussness.entity.Sales;
import com.kgc.warehouse.bussness.service.CustomerService;
import com.kgc.warehouse.bussness.service.GoodsService;
import com.kgc.warehouse.bussness.service.SalesService;
import com.kgc.warehouse.bussness.vo.SalesVO;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("sales")
public class SaleController {

    @Resource
    private SalesService salesService;

    @Resource
    private CustomerService customerService;

    @Resource
    private GoodsService goodsService;

    @RequestMapping("loadAllSales")
    public ResponseResult loadAllSales(SalesVO salesVO){
        Page<Sales> salesPage = new Page<>(salesVO.getPage(),salesVO.getLimit());

        LambdaQueryWrapper<Sales> salesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesLambdaQueryWrapper.eq(salesVO.getCustomerid()!=null&&salesVO.getCustomerid()!=0,Sales::getCustomerid,salesVO.getCustomerid());
        salesLambdaQueryWrapper.eq(salesVO.getGoodsid()!=null&&salesVO.getGoodsid()!=0,Sales::getGoodsid,salesVO.getGoodsid());
        salesLambdaQueryWrapper.ge(salesVO.getStartTime()!=null,Sales::getSalestime,salesVO.getStartTime());
        salesLambdaQueryWrapper.le(salesVO.getEndTime()!=null,Sales::getSalestime,salesVO.getEndTime());
        salesLambdaQueryWrapper.orderByDesc(Sales::getSalestime);

        Page<Sales> page = salesService.page(salesPage, salesLambdaQueryWrapper);
        long total = page.getTotal();
        List<Sales> records = page.getRecords();
        List<Sales> collect = records.stream().map(record -> {
            Customer customer = customerService.getById(record.getCustomerid());
            if (ObjectUtils.isNotNull(customer)) {
                record.setCustomername(customer.getCustomername());
            }
            Goods goods = goodsService.getById(record.getGoodsid());
            if (ObjectUtils.isNotNull(goods)) {
                record.setGoodsname(goods.getGoodsname());
                record.setSize(goods.getSize());
            }
            return record;
        }).collect(Collectors.toList());
        return new ResponseResult(collect,total, CodeMsg.SUCCESS);
    }

    @RequestMapping("addSales")
    public ResponseResult addSales(SalesVO salesVO, HttpSession session){
        try {
            return salesService.addSales(salesVO,session);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.ADD_FAIL);
        }
    }

    @RequestMapping("deleteSales")
    public ResponseResult deleteSales(Integer id){
        try {
            salesService.deleteSales(id);
            return new ResponseResult(CodeMsg.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.DELETE_FAIL);
        }
    }

    @RequestMapping("updateSales")
    public ResponseResult updateSales(SalesVO salesVO){
        try {
            if (salesVO.getNumber()<0) {
                return new ResponseResult(CodeMsg.NUMBER_SALECOUNTS);
            }

            salesService.updateSales(salesVO);
            return new ResponseResult(CodeMsg.UPDATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.UPDATE_FAIL);
        }
    }
}
